#include "LandMarks.h"
#include <osgEarth/JsonUtils>
#include <osgEarthSymbology/Style>
#include <QString>
#include <osgEarthAnnotation/PlaceNode>
#include <qdebug.h>

using namespace osgEarth;

LandMarks::LandMarks()
{

}
                                                                                                   
osgEarth::Symbology::Style* __getIconStyle(const LandMarkItem& item){
    static osgEarth::Symbology::Style* gpstyle = 0;
    if(gpstyle == 0){
        gpstyle = new osgEarth::Symbology::Style[4]();
        gpstyle[0].getOrCreate<IconSymbol>()->url()->setLiteral( "../user_data/cyber/image/index/3.png" );
        gpstyle[0].getOrCreate<IconSymbol>()->declutter() = true;
        gpstyle[0].getOrCreate<IconSymbol>()->scale() = 1.0;
        gpstyle[1].getOrCreate<IconSymbol>()->url()->setLiteral( "../user_data/cyber/image/index/2.png" );
        gpstyle[1].getOrCreate<IconSymbol>()->declutter() = true;
        gpstyle[1].getOrCreate<IconSymbol>()->scale() = 1.0;
        gpstyle[2].getOrCreate<IconSymbol>()->url()->setLiteral( "../user_data/cyber/image/index/1.png" );
        gpstyle[2].getOrCreate<IconSymbol>()->declutter() = true;
        gpstyle[2].getOrCreate<IconSymbol>()->scale() = 1.0;
        gpstyle[3].getOrCreate<IconSymbol>()->url()->setLiteral( "../user_data/cyber/image/index/0.png" );
        gpstyle[3].getOrCreate<IconSymbol>()->declutter() = true;
    }
    if (item.level >= 3) {
        int num = rand() % 254;
        int aa = 30;
        char c[8];
        itoa(num, c, 10);
        std::string url = std::string("../user_data/cyber/image/icons/") + std::string(c) + ".png";
        auto s = new osgEarth::Symbology::Style();
        s->getOrCreate<IconSymbol>()->url()->setLiteral(url);
        s->getOrCreate<IconSymbol>()->declutter() = true;
        s->getOrCreate<IconSymbol>()->scale() = 0.25;
        return s;
    }
    osgEarth::Symbology::Style* sty = gpstyle + item.level;
    sty->getOrCreateSymbol<TextSymbol>()->alignment() = TextSymbol::ALIGN_LEFT_CENTER;
    return sty;
}

Style *__getTextSymbol()
{
    static osgEarth::Symbology::Style* pm = 0;
    if(pm == 0){
        pm = new osgEarth::Symbology::Style();
        pm->getOrCreate<TextSymbol>()->fill() = Color("#ffffff");
        pm->getOrCreate<TextSymbol>()->halo() = Color("#4B0082");
        pm->getOrCreate<TextSymbol>()->declutter() = true;
        pm->getOrCreate<TextSymbol>()->font() = "../user_data/common/font/simhei.ttf";
        pm->getOrCreate<TextSymbol>()->size() = 19;
        pm->getOrCreate<TextSymbol>()->encoding() = TextSymbol::Encoding::ENCODING_UTF8;
        pm->getOrCreate<TextSymbol>()->alignment() =TextSymbol::Alignment::ALIGN_LEFT_BOTTOM_BASE_LINE;
        pm->getOrCreate<TextSymbol>()->layout() =TextSymbol::Layout::LAYOUT_LEFT_TO_RIGHT;
        pm->getOrCreate<TextSymbol>()->pixelOffset() = osg::Vec2s(-25,25);
    }
    return pm;
}

bool LandMarks::parseNode(const char *data, int length, osg::Group *parent, const LFDOption *option)
{
    Json::Reader reader;
    Json::Value root;
    bool res = reader.parse(data,data+length,root);
    if(!res) return false;
    Json::Value& features = root["features"];
    LandMarkItems stas;
    for(int i=0;i<features.size();i++){

        LandMarkItem station;
        Json::Value feature = features[i];
        Json::Value coords = feature["geometry"]["coordinates"];
        int idx = 0;
        station.lon = coords[idx].asDouble();
        station.lat = coords[idx+1].asDouble();
        int count = feature["properties"]["count"].asInt();
        int id = feature["properties"]["id"].asInt();
        station.count = count;
        station.id = id;
        station.level = option->level;
        stas.push_back(station);
    }

    if(stas.size() <=0) {
        return true;
    };

    osgEarth::MapNode* mapNode = getSceneContext()->getMapNode();
	const SpatialReference* geoSRS = getSceneContext()->getSpatialReference();


    for(int i = 0; i < stas.size();++i)
    {
        const LandMarkItem& sta = stas.at(i);
        Style* icon = __getIconStyle(sta);
        Style* text = __getTextSymbol();
        QString cs = QString::number(sta.count).rightJustified(5,'0');
        osgEarth::Annotation::PlaceNode* p2 =
         new osgEarth::Annotation::PlaceNode( GeoPoint(geoSRS, sta.lon, sta.lat), std::string(""), *icon);

        parent->addChild(p2);
        if(sta.level < 3){
            osgEarth::Annotation::PlaceNode* p1 =
             new osgEarth::Annotation::PlaceNode( GeoPoint(geoSRS, sta.lon, sta.lat), cs.toStdString(), *text);
            parent->addChild(p1);
            //ScreenSpaceLayout::activate(p1->getOrCreateStateSet(),14);
        }
    }
    return true;
}

std::string LandMarks::buildUrl(int lv, osgEarth::Bounds &bs)
{
    std::string urlt = "http://116.255.255.137:8088/xeye/dibiao/request?level={z}&bbox={l},{b},{r},{t}";
    replaceIn( urlt, "{z}", Stringify() << (lv+1));
    replaceIn( urlt, "{l}", Stringify() << bs.xMin());
    replaceIn( urlt, "{b}", Stringify() << bs.yMin());
    replaceIn( urlt, "{r}", Stringify() << bs.xMax());
    replaceIn( urlt, "{t}", Stringify() << bs.yMax());
	qDebug() << QString::fromStdString(urlt);
    return urlt;
}


